package org.rency.spring.cloud.boot.mvc.controller;

import org.rency.common.utils.domain.RespBody;
import org.rency.spring.cloud.boot.mvc.domain.MemberDomain;
import org.rency.spring.cloud.boot.mvc.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * $TODO
 *
 * @author: Created by user_rcy@163.com on 2016/08/17.
 * @version: V1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @RequestMapping(value="/infos",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<MemberDomain>> infos(){
        List<MemberDomain> memberList = memberService.loadAll();
        return new ResponseEntity<List<MemberDomain>>(memberList, HttpStatus.OK);
    }

    @RequestMapping(value = "/info/{memberId}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String info(@PathVariable(value = "memberId") String memberId){
        System.out.println("get member by memberId="+memberId);
        MemberDomain member = memberService.get(memberId);
        return member.toString();
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public String add(@RequestBody MemberDomain request){
        System.out.println("register member ="+request);
        MemberDomain member = memberService.add(request);
        return member.toString();
    }

    @RequestMapping(value = "/del/{memberId}",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public RespBody delete(@PathVariable(value = "memberId") String memberId){
        System.out.println("delete member ="+memberId);
        memberService.remove(memberId);
        RespBody respBody = new RespBody();
        respBody.setSuccess(true);
        respBody.setMessage("删除成功");
        return respBody;
    }
}
